package db

import ext.log
import manager.DataBaseManager
import org.json.JSONObject
import java.io.IOException


class Wares(
    val id: String, // 商品ID
    val name: String, // 商品名称
    val price: Double, // 商品价格
    val minImageUrl: String, // 商品图片1
    val maxImageUrl: String, // 商品图片2
    val waveHeatTime: Double, // 商品微波加热时间
    val infraredHeatTime: Double, // 商品红外加热时间
    val heatTempValue: Double, // 商品保温温度
    val starValue: Int, // 商品推荐星级
    val descr: String, // 商品描述
    val factsImageUrl: String, // 商品营养价值
    val heatDissipationTime: Int
) { // 散热时间//

    fun toJson(): JSONObject {
        val json = JSONObject()
        json.put("id", id)
        json.put("name", name)
        json.put("price", price)
        json.put("minImageUrl", minImageUrl)
        json.put("maxImageUrl", maxImageUrl)
        json.put("waveHeatTime", waveHeatTime)
        json.put("infraredHeatTime", infraredHeatTime)
        json.put("heatTempValue", heatTempValue)
        json.put("starValue", starValue)
        json.put("descr", descr)
        json.put("factsImageUrl", factsImageUrl)
        json.put("heatDissipationTime", heatDissipationTime)
        return json
    }

    companion object {

        const val TABLE_NAME = "db_wares"

        const val TABLE_CREATE_SQL = "create table if not exists $TABLE_NAME ( " +
                "id varchar(30) primary key, " +
                "name varchar(100), " +
                "price double, " +
                "min_image_url varchar(1000), " +
                "max_image_url varchar(1000), " +
                "wave_heat_time double, " +
                "infrared_heat_time double, " +
                "heat_temp_value double, " +
                "star_value integer, " +
                "descr varchar(1000), " +
                "facts_image_url varchar(1000), " +
                "heat_dissipation_time integer)"

        const val TABLE_DELETE_ALL_SQL = "delete from $TABLE_NAME"

        const val TABLE_INSERT_SQL = "insert into $TABLE_NAME (" +
                "id, " +
                "name, " +
                "price, " +
                "min_image_url, " +
                "max_image_url, " +
                "wave_heat_time, " +
                "infrared_heat_time, " +
                "heat_temp_value, " +
                "star_value, " +
                "descr, " +
                "facts_image_url, " +
                "heat_dissipation_time) " +
                "values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"

        const val TABLE_QUERY_SQL = "select * from $TABLE_NAME"

        fun withDatabase(): ArrayList<Wares> {
            val list = ArrayList<Wares>()
            val db = DataBaseManager.Instance.readableDatabase
            try {
                val cursor = db.rawQuery(TABLE_QUERY_SQL, null)
                while (cursor.moveToNext()) {
                    val id = cursor.getString(0)
                    val name = cursor.getString(1)
                    val price = cursor.getDouble(2)
                    val minImageUrl = cursor.getString(3)
                    val maxImageUrl = cursor.getString(4)
                    val waveHeatTime = cursor.getDouble(5)
                    val infraredHeatTime = cursor.getDouble(6)
                    val heatTempValue = cursor.getDouble(7)
                    val starValue = cursor.getInt(8)
                    val descr = cursor.getString(9)
                    val factsImageUrl = cursor.getString(10)
                    val heatDissipationTime = cursor.getInt(11)
                    val wares = Wares(
                        id,
                        name,
                        price,
                        minImageUrl,
                        maxImageUrl,
                        waveHeatTime,
                        infraredHeatTime,
                        heatTempValue,
                        starValue,
                        descr,
                        factsImageUrl,
                        heatDissipationTime
                    )
                    log("从数据库找到一个商品:${wares}")
                    list.add(wares)
                }
                cursor.close()
            } catch (e: Exception) {
                e.printStackTrace()
                throw IOException("商品查找失败")
            } finally {
                db.close()
            }
            return list
        }

        fun delete() {
            val db = DataBaseManager.Instance.writableDatabase
            db.execSQL(TABLE_DELETE_ALL_SQL)
            db.close()
            log("商品表数据全部删除")
        }

        fun createTable() {
            val db = DataBaseManager.Instance.writableDatabase
            db.execSQL(TABLE_CREATE_SQL)
            db.close()
            log("商品表创建成功")
        }
    }

    fun insert() {
        val db = DataBaseManager.Instance.writableDatabase
        db.execSQL(
            TABLE_INSERT_SQL, arrayOf(
                id, name, price, minImageUrl, maxImageUrl, waveHeatTime, infraredHeatTime,
                heatTempValue, starValue, descr, factsImageUrl, heatDissipationTime
            )
        )
        db.close()
        log("商品插入成功:$this")
    }

    val priceStr = price.toString()

    override fun toString(): String {
        return "($id $name $price $waveHeatTime $infraredHeatTime $heatTempValue $starValue $descr $heatDissipationTime)"
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as Wares

        if (id != other.id) return false

        return true
    }

    override fun hashCode(): Int {
        return id.hashCode()
    }
}
